﻿#region using
using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Xml.Schema;
#endregion

namespace XmlGenSharp.Logic.Main
{
  public class BoolTypeHelper : TypeHelperBase
  {
    // Singleton

    #region Instance
    public static BoolTypeHelper Instance
    {
      get
      {
        if (_instance == null)
          _instance = new BoolTypeHelper();

        return _instance;
      }
    }
    private static BoolTypeHelper _instance = null;
    #endregion

    // Public Methods

    #region GenerateAttributeLoader(IGeneratorContext cx, ClassGenerator generator, ...)
    public override void GenerateAttributeLoader(IGeneratorContext cx, ClassGenerator generator, string propName, XmlSchemaAttribute attrib, System.Xml.Schema.XmlSchemaUse usage)
    {
      bool defVal = (attrib.DefaultValue != null && (attrib.DefaultValue == "1" || attrib.DefaultValue.ToLower() == "true"));

      if (attrib.Use == XmlSchemaUse.Optional)
        cx.WL("      {0} = XmlUtil.GetBoolAttrib(node, \"{1}\", {2});", propName, attrib.Name, defVal ? "true" : "false");
      else
        cx.WL("      {0} = XmlUtil.GetBoolAttribOrThrow(node, \"{1}\");", propName, attrib.Name);
    }
    #endregion
  }
}
